let baseURL = "https://x.dscmall.cn/api"

function encodeQuery(obj) {
	var arr = [];
	if (obj) {
		for (var key in obj) {
			if (obj.hasOwnProperty(key)) {
				var value = obj[key];
				if (value != null)
					arr.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
			}
		}
	}
	return arr.join("&");
}

// get异步请求封装
function get(url) {
	var promise = new Promise((resolve, reject) => {
		wx.request({
			url: baseURL+url,
			method: "GET",
			dataType: 'json',
			success: function(res) {
				if (res.statusCode === 200)
					resolve(res.data);
				else {
					console.log(res, url)
					wx.showToast({
						title: '服务器请求失败'
					});
				}

			},
			fail: function(e) {
				wx.showToast({
					title: '服务器请求失败'
				});
			}
		});
	});
	return promise;
}

// post异步请求封装
function post(url, data) {
	var promise = new Promise((resolve, reject) => {
		wx.request({
			url: baseURL+url,
			method: 'POST',
			data: data,
			dataType: 'json',
			// header: {
			// 	'Content-Type': 'application/json',
			// 	'Token': utility.getToken()
			// },
			success: function(res) {
				if (res.statusCode === 200)
					resolve(res.data);
				
				else {
					console.log(res, url)
					uni.showToast({
						title: '服务器请求失败'
					});
				}

			},
			fail: function(e) {
				uni.showToast({
					title: '服务器请求失败'
				});
			}
		})
	});
	return promise;
}
module.exports = {
  encodeQuery,
  get,
  post
}